package com.lgc.webtest.configuration.dataSource;


import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源配置
 */
//@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties(prefix="datasource.payment")
    public DataSource paymentMasterDataSource() {
        return  DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="datasource.paymentslave")
    public DataSource paymentSlaveDataSource() {
        return  DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="datasource.paymentpartner")
    public DataSource partnerMasterDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="datasource.paymentpartnerslave")
    public DataSource partnerSlaveDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    public DataSource myRoutingDataSource(@Qualifier("paymentMasterDataSource") DataSource paymentMasterDataSource,
                                          @Qualifier("paymentSlaveDataSource") DataSource paymentSlaveDataSource,
                                          @Qualifier("partnerMasterDataSource") DataSource partnerMasterDataSource,
                                          @Qualifier("partnerSlaveDataSource") DataSource partnerSlaveDataSource) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DBTypeEnum.PARTNER_WRITE, paymentMasterDataSource);
        targetDataSources.put(DBTypeEnum.PARTNER_READ, paymentSlaveDataSource);
        targetDataSources.put(DBTypeEnum.PARTNER_WRITE, partnerMasterDataSource);
        targetDataSources.put(DBTypeEnum.PARTNER_READ, partnerSlaveDataSource);

        MyRoutingDataSource myRoutingDataSource = new MyRoutingDataSource();
        myRoutingDataSource.setDefaultTargetDataSource(paymentMasterDataSource);
        myRoutingDataSource.setTargetDataSources(targetDataSources);
        return myRoutingDataSource;
    }
}
